package com.jd.basic.controller;

import com.jd.basic.dto.SmsCodeDto;
import com.jd.basic.exception.BusinessException;
import com.jd.basic.service.IVerifyCodeService;
import com.jd.basic.util.AjaxsResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;



    @GetMapping("/image/{key}")
    public AjaxsResult image(@PathVariable("key")String key){
        try {
            // 调用service层获取图形验证码
            String base64Str = verifyCodeService.image(key);
            // 将获取的base64码返回给前端
            return AjaxsResult.me().setResultObj(base64Str);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxsResult(false,"图形验证失败");
        }
    }

    @PostMapping("/smsCode")
    public AjaxsResult smsCode(@RequestBody SmsCodeDto smsCodeDto){
        try {
            verifyCodeService.smsCode(smsCodeDto);
            return new AjaxsResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxsResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxsResult(false,"获取手机验证码失败");
        }
    }

    @PostMapping("/binderSmsCode")
    public AjaxsResult binderSmsCode(@RequestBody SmsCodeDto smsCodeDto){
        try {
            verifyCodeService.binderSmsCode(smsCodeDto);
            return new AjaxsResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxsResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxsResult(false,"获取手机验证码失败");
        }
    }
}
